module.exports = {
	/**
	 * 修改数据
	 * @url agent/toufang/kh/update 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @params {Number} code 错误码，0表示成功
	 * @params {String} msg 详细信息
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid
		} = data;
		let res = {
			code: 0,
			msg: 'ok'
		};
		// 业务逻辑开始-----------------------------------------------------------
		// 获取前端传过来的参数
		let {
			_id,
			bind_first_recharge,
			bind_first_recharge_id,
			bind_again_recharge_id,
			bind_again_recharge,
			bind_repurchase_id,
			setting_czmb,
			setting_again_czmb,
			setting_kdpz,
			ad_id,
			desc,
			tfurl,
			middleman_id,
			agentId,
			agent_username,
			appid,
			appname,
			tv_id,
			tv_name,
			series,
			follow,
			gzhId,
			path,
			album_id,
			tfid,
			platform,
			setting_agent
		} = data;
		// 参数验证开始
		if (vk.pubfn.isNull(_id)) return {
			code: -1,
			msg: 'id不能为空'
		};

		//校验一下填写的appid有没有对应的秘钥存在数据库里，没有的话就修改失败
		let appinfo = await db.collection('a_appid').where({
			appid: appid
		}).get()
		try {
			console.log("data=", data._id);
			//通过data._id获取旧数据
			let old_datas = await db.collection('a_dy_tfurl').doc(data._id).get()
			//记录操作日志到数据库
			let addlogs = await vk.baseDao.add({
				dbName: "opendb-admin-log",
				dataJson: {
					type: "update",
					path: "admin/dls/kh/dytflj/update",
					edit_log: data, //本次修改了哪些内容
					obj_id: old_datas.data[0]._id,
					old_data: old_datas.data[0],
					new_data: data,
					user_id: userInfo._id,
					bind_repurchase_id,
					user_name: userInfo.username,
					nick_name: userInfo.nickname,
					ip: originalParam.context.CLIENTIP,
					ua: originalParam.context.CLIENTUA
				}
			});
			// 业务逻辑开始-----------------------------------------------------------
		} catch (err) {
			console.log("日志加入失败");
		}
		if (appinfo.data.length == 0) {
			res.code = 1
			res.msg = "未找到appid对应的appsecret,请先去小程序配置--小程序管理添加"
			return res;
		}

		// 根据aid series 查询 eid
		let epid = await vk.baseDao.select({
			dbName: "a_tv_series", // 表名
			whereJson: { // 条件
				series: series,
				album_id: album_id
			},
		});
		console.log("epid80===", epid.rows[0].episode_id);
		let episode_id = epid.rows[0].episode_id

		const purl = "pages/index/index?path=player&tv_id=" + tv_id + "&series=" + series +
			"&invite_code=" + agentId + "&tfid=" + _id + "&middleman_id=" + middleman_id +
			"&tt_album_id=" + album_id + "&tt_episode_id=" + episode_id;

		console.log("purl===", purl);

		if (!setting_agent) {
			setting_agent = ""
		}
		// 参数验证结束
		let dbName = "a_dy_tfurl"; // 表名
		// 执行 数据库 updateById 命令
		await vk.baseDao.updateById({
			dbName,
			id: _id,
			dataJson: {
				ad_id,
				bind_first_recharge,
				bind_first_recharge_id,
				bind_again_recharge_id,
				bind_again_recharge,
				bind_repurchase_id,
				setting_czmb,
				setting_again_czmb,
				setting_kdpz,
				desc,
				tfurl,
				xcxpath: "pages/index/index?path=player&tv_id=" + tv_id + "&series=" + series +
					"&invite_code=" + agentId + "&tfid=" + _id + "&middleman_id=" + middleman_id +
					"&tt_album_id=" + album_id + "&tt_episode_id=" + episode_id,
				agentId,
				agent_username,
				appid,
				appname,
				tv_id,
				tv_name,
				series,
				follow,
				gzhId,
				path,
				tfid,
				platform,
				setting_agent
			}
		});
		let resaa = await vk.baseDao.getTableData({
			dbName: "a_klmb",
			whereJson: {
				_id: setting_kdpz
			}

		});

		console.log("在创建时获取扣量模板数据", resaa.rows);

		let huichuan = resaa.rows[0]
		// let adds = await db.collection('a_config').add({
		// 	name: "RebackPercent",
		// 	tfid: res.id,
		// 	agentId: userInfo._id,
		// 	proportion_value: 0,
		// 	huichuan: resaa.rows,
		// 	add_date: new Date().getTime()
		// })

		await vk.baseDao.update({
			dbName: "a_config", // 表名
			whereJson: { // 条件
				tfid: _id,
			},
			dataJson: { // 需要修改的数据
				mid: huichuan._id,
				koulianbz: huichuan.huichuana.koulianbz,
				koulianmc: huichuan.huichuana.koulianmc,
				lqdhcbs: huichuan.huichuana.lqdhcbs,
				newuser: huichuan.huichuana.newuser,
				amountRanges: huichuan.amountRanges,
				douyinRadio: huichuan.huichuana.douyinRadio,
				firstorder: huichuan.huichuana.firstorder,
				hour_backhaul: huichuan.huichuana.hour_backhaul,
				update_date: new Date().getTime()
			}
		});


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}